package models

import (
	"base_srv/basic/config"
	"time"
)

type DriverAppUpdate struct {
	Id               uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	DriverAppName    string    `gorm:"column:driver_app_name;type:varchar(30);comment:司机端app名称;not null;" json:"driver_app_name"`                       // 司机端app名称
	DriverAppVersion string    `gorm:"column:driver_app_version;type:varchar(20);comment:司机端app版本号;not null;" json:"driver_app_version"`                // 司机端app版本号
	LaunchSystem     int8      `gorm:"column:launch_system;type:tinyint(1);comment:上线系统 1-全部 2-iOS 3-Android;not null;default:1;" json:"launch_system"` // 上线系统 1-全部 2-iOS 3-Android
	IsForceUpdate    int8      `gorm:"column:is_force_update;type:tinyint(1);comment:是否强制更新 1-是 2-否;not null;" json:"is_force_update"`                  // 是否强制更新 1-是 2-否
	UpdateText       string    `gorm:"column:update_text;type:varchar(50);comment:更新文本;not null;" json:"update_text"`                                   // 更新文本
	UserId           int32     `gorm:"column:user_id;type:int;not null;" json:"user_id"`
	UploadTime       time.Time `gorm:"column:upload_time;type:datetime;comment:上线时间;not null;" json:"upload_time"`               // 上线时间
	StartUpdateTime  time.Time `gorm:"column:start_update_time;type:datetime;comment:开始更新时间;not null;" json:"start_update_time"` // 开始更新时间
	CreateAt         time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt         time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt         time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
}

func (d *DriverAppUpdate) TableName() string {
	return "driver_app_update"
}
func (d *DriverAppUpdate) Create() error {
	return config.DB.Create(&d).Error
}
func (d *DriverAppUpdate) Update() error {
	return config.DB.Updates(&d).Error
}
func (d *DriverAppUpdate) Delete() error {
	return config.DB.Delete(&d).Error
}
func (d *DriverAppUpdate) UpdateList() (list []DriverAppUpdateResp, err error) {
	err = config.DB.Model(&DriverAppUpdate{}).Select("driver_app_update.id,driver_app_update.driver_app_name,driver_app_update.driver_app_version,driver_app_update.launch_system," +
		"driver_app_update.is_force_update,driver_app_update.update_text,driver_app_update.upload_time,driver_app_update.start_update_time,user.real_name").
		Joins("left join user on user.id = driver_app_update.user_id").
		Where("driver_app_update.delete_at is null").Order("driver_app_update.id desc").Find(&list).Error
	return
}

type DriverAppUpdateResp struct {
	Id               uint32    `json:"id,omitempty"`
	DriverAppName    string    `json:"driverAppName,omitempty"`
	DriverAppVersion string    `json:"driverAppVersion,omitempty"`
	LaunchSystem     int8      `json:"launchSystem,omitempty"`
	IsForceUpdate    int8      `json:"isForceUpdate,omitempty"`
	UpdateText       string    `json:"updateText,omitempty"`
	UploadTime       time.Time `json:"uploadTime"`
	StartUpdateTime  time.Time `json:"startUpdateTime"`
	RealName         string    `json:"realName,omitempty"`
}
